// pages/home/productDetail/productDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tit: '',
    proImgArr:[0,1,2,3],
    pInfo:{},//产品数据
    sInfo:{},//商户数据
    imgURL: app.globalData.imgURL,//图片总路径

  },

  imgClick:function(e){
    var that = this;
    console.log(e);
    console.log(e.currentTarget.id);
    var imgArr = that.data.pInfo.pro_img;
    var nImgArr = new Array();
    for (var i = 0; i < imgArr.length; i++) {
      nImgArr[i] = that.data.imgURL + imgArr[i].replace('_thum', '');
    }
    console.log(nImgArr);
    wx.previewImage({
      current: nImgArr[e.currentTarget.id],
      urls: nImgArr,
    })
  },


  //头像点击
  iconClick:function(e){
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '../shopMessage/shopMessage?id=' + e.currentTarget.id,
    })
  },
  
  phoneClick: function (e) {
    console.log(e.currentTarget.id);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log('产品详情');
    console.log(options.tit);
    console.log(options.id);
    console.log(options.openid);
    if (options.tit){
      this.setData({
        tit: options.tit,
      })
    }
    that.requestData(options.id, options.openid);
  },

  requestData: function (value,openid) {
    var that = this;
    console.log('最新产品 优惠促销 人气商家 请求');

    app.post('pro/queryProduceInfoForShop', { id: value, openId: openid}).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        var nPInfo = res.produceInfo;
        nPInfo.pro_img = nPInfo.pro_img.split(',');

        that.setData({
          sInfo:res.shopInfo,
          pInfo: nPInfo,
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})